﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BlinkyDevice
{
    public partial class BlinkyUI : UsbSearchForm
    {
        private BlinkyControl devControl;
        private Blinky blinkyDevice = null;

        public BlinkyUI()
        {
            InitializeComponent();
            CheckIfBlinkyConnected();
        }

        private void CheckIfBlinkyConnected()
        {
            blinkyDevice = Blinky.findBlinkyDevice();	// look for the device on the USB bus
            if (blinkyDevice != null)	// did we find it?
            {
                // Yes! So wire into its events and update the UI
                blinkyDevice.OnDeviceRemoved += new EventHandler(BlinkyDevice_OnDeviceRemoved);
                blinkyDevice.OnLightChanged += new BlinkyLightChangedEventHandler(BlinkyDevice_OnLightChanged);
                UpdateDeviceEnabled();
            }
            UpdateDeviceEnabled();
        }

        private void BlinkyDevice_OnDeviceRemoved(object sender, EventArgs e)
        {
            blinkyDevice = null;
            if (InvokeRequired)
            {
                Invoke(new EventHandler(BlinkyDevice_OnDeviceRemoved), new object[] { sender, e });
            }
            else
            {
                UpdateDeviceEnabled();
            }
        }

        private void BlinkyDevice_OnLightChanged(object sender, BlinkyLightChangedEventArgs args)
        {
            if (InvokeRequired)
            {
                Invoke(new BlinkyLightChangedEventHandler(BlinkyDevice_OnLightChanged), new object[] { sender, args });
            }
            else
            {
                devControl.RedOn = args.Lights.red;
                devControl.GreenOn = args.Lights.green;
            }
        }

        private void Blinky_OnRedOnChanged(object sender, RedOnChangedArgs args)
        {
            if (InvokeRequired)
            {
                Invoke(new RedOnChangedEventHandler(Blinky_OnRedOnChanged), new object[] { sender, args });
            }
            else
            {
                RefreshRedLabel();
            }
        }

        private void Blinky_OnGreenOnChanged(object sender, GreenOnChangedArgs args)
        {
            if (InvokeRequired)
            {
                Invoke(new GreenOnChangedEventHandler(Blinky_OnGreenOnChanged), new object[] { sender, args });
            }
            else
            {
                RefreshGreenLabel();
            }
        }

        private void RefreshRedLabel()
        {
            if (blinkyDevice != null)
            {
                blinkyDevice.setLights(1);
            }
        }

        private void RefreshGreenLabel()
        {
            if (blinkyDevice != null)
            {
                blinkyDevice.setLights(2);
            }
        }

        private void UpdateDeviceEnabled()
        {
            bool bEnabled = false;
            if (blinkyDevice != null)
            {
                bEnabled = true;
            }
            devControl.Enabled = bEnabled;
        }

        protected override void OnDeviceArrived(EventArgs args)
        {
            base.OnDeviceArrived(args);
            if (blinkyDevice == null)
            {
                CheckIfBlinkyConnected();
            }
        }

        [STAThread]
        static void Main()
        {
            Application.Run(new BlinkyUI());
        }
    }
}
